<template>
  <div class="auth-container">
    <!-- 背景装饰 -->
    <div class="auth-background">
      <div class="bg-decoration"></div>
    </div>

    <!-- 登录卡片 -->
    <div class="auth-card">
      <!-- Logo 和标题区域 -->
      <div class="auth-header">
        <div class="logo-section">
          <!-- 小橘 Logo -->
          <div class="logo">
            <svg width="40" height="40" viewBox="0 0 40 40" fill="none">
              <circle cx="20" cy="20" r="18" fill="var(--orange-primary)" />
              <circle cx="15" cy="16" r="2.5" fill="white" />
              <circle cx="25" cy="16" r="2.5" fill="white" />
              <path d="M13 25 Q20 30 27 25" stroke="white" stroke-width="2" stroke-linecap="round" />
            </svg>
          </div>
          <h1 class="platform-title">小橘运维</h1>
        </div>
        <p class="platform-subtitle">WISE-IoT</p>
      </div>

      <!-- 登录表单 -->
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="auth-form"
        @submit.prevent="handleLogin">
        <!-- 用户名输入框 -->
        <el-form-item prop="username" class="form-item">
          <el-input v-model="loginForm.username" placeholder="用户名" size="large" class="auth-input" :prefix-icon="User"
            clearable />
        </el-form-item>

        <!-- 密码输入框 -->
        <el-form-item prop="password" class="form-item">
          <el-input v-model="loginForm.password" type="password" placeholder="密码" size="large" class="auth-input"
            :prefix-icon="Lock" show-password clearable />
        </el-form-item>

        <!-- 记住我和忘记密码 -->
        <div class="form-options">
          <el-checkbox v-model="loginForm.rememberMe" class="remember-checkbox">
            记住我
          </el-checkbox>
          <el-link type="primary" :underline="false" class="forgot-link" @click="goToForgetPassword">
            忘记密码？
          </el-link>
        </div>

        <!-- 登录按钮 -->
        <el-form-item class="form-item">
          <el-button type="primary" size="large" class="auth-button" :loading="isLoading" @click="handleLogin"
            native-type="submit">
            {{ isLoading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 注册链接 -->
      <div class="auth-footer">
        <span class="footer-text">还没有账号？</span>
        <el-link type="primary" :underline="false" class="register-link" @click="goToRegister">
          立即注册
        </el-link>
      </div>
    </div>

    <!-- 页脚版权信息 -->
    <div class="copyright">
      <p>Powered by WISE-IoT</p>
      <el-link type="info" :underline="false" class="language-switch">
        简体中文 ▼
      </el-link>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import type { LoginForm } from './types'

// 路由实例
const router = useRouter()

// 表单引用
const loginFormRef = ref<FormInstance>()

// 响应式数据
const isLoading = ref(false)

// 登录表单数据
const loginForm = reactive<LoginForm>({
  username: '',
  password: '',
  rememberMe: false
})

// 表单验证规则
const loginRules: FormRules<LoginForm> = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

/**
 * 处理登录提交
 */
const handleLogin = async (): Promise<void> => {
  if (!loginFormRef.value) return

  try {
    // 表单验证
    await loginFormRef.value.validate()

    // 开始加载状态
    isLoading.value = true

    // 模拟登录API调用
    await new Promise(resolve => setTimeout(resolve, 1500))

    // 这里应该调用实际的登录API
    console.log('登录信息:', {
      username: loginForm.username,
      password: loginForm.password,
      rememberMe: loginForm.rememberMe
    })

    // 登录成功提示
    ElMessage.success('登录成功！')

    // 跳转到仪表板
    await router.push('/dashboard')

  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error('登录失败，请检查用户名和密码')
  } finally {
    // 结束加载状态
    isLoading.value = false
  }
}

/**
 * 跳转到注册页面
 */
const goToRegister = (): void => {
  router.push('/register')
}

/**
 * 跳转到忘记密码页面
 */
const goToForgetPassword = (): void => {
  router.push('/forget-password')
}
</script>

<style scoped>
.auth-container {
  position: relative;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fa;
  padding: 20px;
}


.auth-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
}

.bg-decoration {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image:
    radial-gradient(circle at 20% 20%, rgba(255, 127, 0, 0.08) 0%, transparent 50%),
    radial-gradient(circle at 80% 80%, rgba(255, 127, 0, 0.06) 0%, transparent 50%);
  background-size: 600px 600px;
}


.auth-card {
  position: relative;
  z-index: 2;
  width: 100%;
  max-width: 400px;
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(255, 127, 0, 0.1);
  padding: 40px 32px;
  margin-bottom: 40px;
}


.auth-header {
  text-align: center;
  margin-bottom: 32px;
}

.logo-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
}

.logo {
  display: flex;
  align-items: center;
  justify-content: center;
}

.platform-title {
  font-size: 24px;
  font-weight: 600;
  color: var(--orange-primary);
  margin: 0;
  letter-spacing: 1px;
}

.platform-subtitle {
  font-size: 14px;
  color: #666;
  margin: 0;
  font-weight: 500;
  letter-spacing: 2px;
}


.auth-form {
  width: 100%;
}

.form-item {
  margin-bottom: 20px;
}

.form-item:last-child {
  margin-bottom: 0;
}


:deep(.auth-input .el-input__wrapper) {
  border-radius: 8px;
  border: 1px solid #e0e6ed;
  box-shadow: none;
  transition: all 0.3s ease;
}

:deep(.auth-input .el-input__wrapper:hover) {
  border-color: var(--orange-primary);
}

:deep(.auth-input.is-focus .el-input__wrapper) {
  border-color: var(--orange-primary);
  box-shadow: 0 0 0 2px rgba(255, 127, 0, 0.1);
}

:deep(.auth-input .el-input__inner) {
  font-size: 14px;
  color: #333;
}

:deep(.auth-input .el-input__inner::placeholder) {
  color: #999;
}

/* 表单选项 */
.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 16px 0 24px 0;
}

:deep(.remember-checkbox .el-checkbox__label) {
  font-size: 14px;
  color: #666;
}

:deep(.remember-checkbox .el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: var(--orange-primary);
  border-color: var(--orange-primary);
}

.forgot-link {
  font-size: 14px;
  color: var(--orange-primary) !important;
}

.forgot-link:hover {
  color: var(--orange-primary-dark) !important;
}

/* 登录按钮 */
:deep(.auth-button) {
  width: 100%;
  height: 44px;
  background-color: var(--orange-primary);
  border-color: var(--orange-primary);
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
}

:deep(.auth-button:hover) {
  background-color: var(--orange-primary-dark);
  border-color: var(--orange-primary-dark);
}

:deep(.auth-button:focus) {
  background-color: var(--orange-primary);
  border-color: var(--orange-primary);
}

/* 页脚 */
.auth-footer {
  text-align: center;
  margin-top: 24px;
  padding-top: 20px;
  border-top: 1px solid #f0f0f0;
}

.footer-text {
  font-size: 14px;
  color: #666;
  margin-right: 8px;
}

.register-link {
  font-size: 14px;
  color: var(--orange-primary) !important;
  font-weight: 500;
}

.register-link:hover {
  color: var(--orange-primary-dark) !important;
}

/* 版权信息 */
.copyright {
  position: relative;
  z-index: 2;
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  max-width: 400px;
  font-size: 12px;
  color: #999;
}

.copyright p {
  margin: 0;
}

.language-switch {
  font-size: 12px;
  color: #999 !important;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .auth-card {
    padding: 32px 24px;
    margin: 16px;
  }

  .platform-title {
    font-size: 20px;
  }

  .form-options {
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
  }
}

@media (max-width: 360px) {
  .auth-container {
    padding: 12px;
  }

  .auth-card {
    padding: 24px 20px;
  }
}
</style>
